package com.codes.controller;

import com.codes.core.common.result.Result;
import com.codes.service.InformationSV;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2020/6/10 10:14
 * @Created by peanut
 */

@Slf4j
@Api(tags = "资讯接口")
@RestController
@RequestMapping("/information")
public class InformationCtr {

    @Autowired
    private InformationSV informationSV;

    @ApiOperation("添加一条资讯")
    @PostMapping("/add")
    public Result addInform(@RequestParam Map<String,Object> map){
        int re = 0;
        Integer id = MapUtils.getInteger(map,"id");
        if(null != id){
            re = informationSV.updateByPrimaryKeySelective(map);
            return Result.success("修改成功",re);
        }
        re = informationSV.insertSelective(map);
        if(re > 0) return Result.success("添加成功",re);
        return Result.failed("添加失败");

    }

    @ApiOperation("资讯分页pc")
    @GetMapping("/page")
    public Result pageInformation(@RequestParam( defaultValue="1",required=true) int   page,
                                  @RequestParam( defaultValue="10",required=true) Integer limit,
                                  @RequestParam Map<String,Object> map ) {
        PageHelper.startPage(page,limit);
        List<Map<String, Object>> list =informationSV.getList(map);
        PageInfo<Map<String, Object>> info = new PageInfo< >(list);
        return Result.pages(info.getList(),(int)info.getTotal());
    }

    @ApiOperation("资讯分页wx")
    @GetMapping("/pagewx")
    public Result pageInformationWx( @RequestBody Map<String,Object> map ) {

        Integer page = MapUtils.getInteger(map,"page");
        Integer limit = MapUtils.getInteger(map,"limit");
        PageHelper.startPage(page,limit);
        List<Map<String, Object>> list =informationSV.getList(map);
        PageInfo<Map<String, Object>> info = new PageInfo< >(list);
        return Result.pages(info.getList(),(int)info.getTotal());
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    public Result delete(@RequestParam Map<String,Object> map){
        Integer id = MapUtils.getInteger(map,"id");
        if( null != id){
            int re = informationSV.deleteByPrimaryKey(id);
            if(re >0) return Result.success("删除成功",re);
        }
        return Result.failed("删除失败");
    }


}